package novel.common.utils.qiniu;

import com.qiniu.common.QiniuException;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.model.FetchRet;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import novel.common.constant.IConstInfo;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class OnlineUploadServiceImpl implements OnlineUploadService {

    @Override
    public String uploadUrl(String remoteSrcUrl) {
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region1());
        //...其他参数参考类注释
        String key = "pic" + System.currentTimeMillis();
        Auth auth = Auth.create(IConstInfo.accesskey, IConstInfo.secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        //抓取网络资源到空间
        try {
            FetchRet fetchRet = bucketManager.fetch(remoteSrcUrl, IConstInfo.bucketName, key);
            return fetchRet.key;
        } catch (QiniuException ex) {
           log.error("uploadUrl error0 {}",ex.getMessage(),ex);
        }
        return null;
    }

    public static void main(String[] args) {
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region1());
       //...其他参数参考类注释
        String key = "pic" + System.currentTimeMillis();
        String remoteSrcUrl = "http://devtools.qiniu.com/qiniu.png";
        Auth auth = Auth.create(IConstInfo.accesskey, IConstInfo.secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        //抓取网络资源到空间
        try {
            FetchRet fetchRet = bucketManager.fetch(remoteSrcUrl, IConstInfo.bucketName, key);
            System.out.println(fetchRet.hash);
            System.out.println(fetchRet.key);
            System.out.println(fetchRet.mimeType);
            System.out.println(fetchRet.fsize);
        } catch (QiniuException ex) {
            System.err.println(ex.response.toString());
        }
    }

    @Override
    public String getUrl(String key) {
        return null;
    }
}
